package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.CartCheckedOutVO;
import com.cskaoyan.bean.vo.CartListVO;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 小程序端应用-购物车模块
 * 创建日期: 2022/04/08 21:03
 *
 * @author HanWeiqi
 */
@RestController
@RequestMapping("wx/cart")
public class CartController {
    @Autowired
    CartService cartService;

    // 获取购物车商品件数
    @RequestMapping("goodscount")
    public BaseRespVo cartGoodsCount(){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        if (marketUser == null){
            return BaseRespVo.ok(0);
        }
        Integer count = cartService.goodsCount(marketUser.getId());
        return BaseRespVo.ok(count);
    }

    // 添加商品到购物车
    @PostMapping("add")
    public BaseRespVo cartAdd(@RequestBody CartAddBO cartAddBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        if (marketUser == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        int count = cartService.cartAdd(cartAddBO,marketUser);
        if(count == 1){
            Integer cartCount = cartService.goodsCount(marketUser.getId());
            return BaseRespVo.ok(cartCount);
        }
        else if(count == 404){
            return BaseRespVo.fail(700,"商品库存不足");
        }
        else{
            return BaseRespVo.fail(502,"系统内部错误");
        }

    }

    // 获取购物车的数据
    @RequestMapping("index")
    public BaseRespVo cartIndex(){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        CartListVO cartListVO = cartService.cartIndex(marketUser);
        return BaseRespVo.ok(cartListVO);

    }

    // 更新购物车的商品
    @PostMapping("update")
    public BaseRespVo cartUpdate(@RequestBody CartUpdateBO cartUpdateBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        int count = cartService.cartUpdate(cartUpdateBO,marketUser);
        if(count == 404){
            return BaseRespVo.fail(700,"商品库存不足");
        }else if(count == 1){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail(700,"系统内部错误");
        }

    }

    // 选择或取消选择商品
    @PostMapping("checked")
    public BaseRespVo cartChecked(@RequestBody CartCheckedBO cartCheckedBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        CartListVO cartListVO = cartService.cartChecked(cartCheckedBO,marketUser);
        return BaseRespVo.ok(cartListVO);
    }

    // 立即购买商品
    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody CartFastAddBO cartFastAddBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        if (marketUser == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        Integer cartId = cartService.fastAdd(cartFastAddBO,marketUser);
        return BaseRespVo.ok(cartId);
    }


    // 删除购物车的商品
    @PostMapping("delete")
    public BaseRespVo cartDelete(@RequestBody CartDeleteBO cartDeleteBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        CartListVO cartListVO = cartService.cartDelete(cartDeleteBO,marketUser);
        return BaseRespVo.ok(cartListVO);
    }

    // 下单前信息确认
    @RequestMapping("checkout")
    public  BaseRespVo cartCheckout(CartCheckedoutBO checkedoutBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        if (marketUser == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        CartCheckedOutVO cartCheckedOutVO = cartService.checkedOut(checkedoutBO,marketUser);
        return BaseRespVo.ok(cartCheckedOutVO);
    }

}
